package com.ynunicom.midplatform.common.persistence.entity;

import com.ynunicom.midplatform.sys.user.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.data.domain.AuditorAware;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * Spring审计功能实现：提供当前审计者
 *
 * @author bobatkm Oct 28, 2017
 */
@Component
public class AuditorProvider implements AuditorAware<SysUser> {

    @NonNull
    @Override
    public Optional<SysUser> getCurrentAuditor() {
        Subject subject = SecurityUtils.getSubject();
        SysUser sysUser = (SysUser) subject.getPrincipals().getPrimaryPrincipal();
        return Optional.ofNullable(sysUser);
    }
}
